package com.penghaisoft.microwms.base.web.auth.service.impl;

import com.penghaisoft.microwms.base.web.auth.service.IBaseUserDataauthTypeService;
import com.penghaisoft.microwms.base.web.dataauth.dao.BaseUserDataauthTypeMapper;
import com.penghaisoft.microwms.common.base.model.BaseUserDataauthType;
import com.penghaisoft.microwms.common.dto.KeyValueObj;
import com.penghaisoft.microwms.common.dto.Resp;
import com.penghaisoft.microwms.common.page.Pager;
import com.penghaisoft.microwms.common.service.BaseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 业务逻辑实现类
 * </p>
 * 
 * @author
 * @createDate 
 **/
@Slf4j
@Service("baseUserDataauthTypeService")
public class BaseUserDataauthTypeServiceImpl extends BaseService implements IBaseUserDataauthTypeService {
	@Resource
	private BaseUserDataauthTypeMapper baseUserDataauthTypeMapper;

	/**
	* 新增记录
	* @param baseUserDataauthType
	* @return
	*/
	@Override
	public Resp create(BaseUserDataauthType baseUserDataauthType){
		baseUserDataauthType.setActiveFlag("1");
		int count = baseUserDataauthTypeMapper.checkSaveUnique(baseUserDataauthType);
		if (count==0) {
			
			baseUserDataauthTypeMapper.create(baseUserDataauthType);
			return success();
		}else {
			return fail("base.auth.userdataauthtype.normaluserbuztype.conflict");
		}
	}

	/**
	* 删除记录（逻辑删除）
	* @param baseUserDataauthType
	* @return
	*/
	@Override
	public Resp delete(BaseUserDataauthType baseUserDataauthType){
		baseUserDataauthTypeMapper.delete(baseUserDataauthType);
		return success();
	}

	/**
	* 查询列表
	* @param page rows
	* @param condition
	* @return
	*/
	@Override
	public Pager<BaseUserDataauthType> findListByCondition(int page, int rows, BaseUserDataauthType condition){
		Pager<BaseUserDataauthType> pager = new Pager<>();
		pager.setPage(page);
		pager.setRows(rows);
		condition.preQuery();
		List<BaseUserDataauthType> records = baseUserDataauthTypeMapper.queryList(pager,condition);
		long size = baseUserDataauthTypeMapper.queryCount(condition);
		pager.setRecords(records);
		pager.setTotalCount(size);
		return pager;
	}

	/**
	* 查询单条
	* @param id
	* @return
	*/
	@Override
	public BaseUserDataauthType findById(String id){
		return baseUserDataauthTypeMapper.queryById(id);
	}

	/**
	* 修改记录
	* @param baseUserDataauthType
	* @return
	*/
	@Override
	public Resp update(BaseUserDataauthType baseUserDataauthType){
		int count = baseUserDataauthTypeMapper.checkSaveUnique(baseUserDataauthType);
		if (count==0) {
			
			baseUserDataauthTypeMapper.updateBySelect(baseUserDataauthType);
			return success();
		}else {
			return fail("base.auth.userdataauthtype.normaluserbuztype.conflict");
		}
	}

	/**
	* <p>Title: queryAll</p> 
	* <p>Description: 查询所有可用的数据权限类型</p> 
	* @return 
	* @see com.haier.cosmo.base.auth.dataauth.service.IBaseUserDataauthTypeService#queryAll() 
	*/
	@Override
	public List<BaseUserDataauthType> queryAll() {
		return baseUserDataauthTypeMapper.queryActiveAll();
	}

	/** 
	* <p>Title: getTabAllList</p> 
	* <p>Description:获取单个tab下的所有选择数据 </p> 
	* @param type
	* @return 
	* @see com.haier.cosmo.base.auth.dataauth.service.IBaseUserDataauthTypeService#getTabAllList(com.haier.cosmo.base.auth.model.BaseUserDataauthType) 
	*/
	@Override
	public List<KeyValueObj> getTabAllList(BaseUserDataauthType type) {
		List<KeyValueObj> tabAllList = new ArrayList<>();
		StringBuffer sqlBuf = new StringBuffer();
		if (type.getIsComplex().equals("0")) {
			sqlBuf.append("SELECT ").append(type.getColumnCode()).append(" as 'key', ");
			sqlBuf.append(type.getColumnName()).append(" as 'value' from ");
			sqlBuf.append(type.getTableName());
			String whereClause = type.getWhereClause();
			if (whereClause!=null&&!whereClause.equals("")) {
				if (whereClause.indexOf("$1")>-1) {
					whereClause = whereClause.replace("$1", type.getUserDefined1());
				}
				String whereSql = " where " + whereClause;
				sqlBuf.append(whereSql);
			}
		}
		if (type.getIsComplex().equals("1")) {
			sqlBuf.append(type.getUserSql());
		}
		String sql = sqlBuf.toString();
		log.info("动态数据权限sql=="+sql);
		tabAllList = baseUserDataauthTypeMapper.exeDynamicSql(sql);
		return tabAllList;
	}
	

}
